import { fetchPreKey } from '@/api/create'

import MeetingHeader from '@/components/MeetingHeader'
import WebRecommendBrowser from '@/components/WebRecommendBrowser'
import useInit from '@/hooks/useInit'
import { useGlobalStore } from '@/store/global'
import { getUrlParams } from '@/utils'
import { useEffect } from 'react'
import shallow from 'zustand/shallow'
import { useCreateContentStore } from '../create/createContent/store'
import { useBasicInfoStore } from '../create/createContent/blocks/basicInfo/store'
import { useMeetingParamsStore } from '../create/createContent/blocks/meetingParams/store'
import { useMeetingMixStore } from '../create/createContent/blocks/mix/store'
import { useMeetingDcsStore } from '../create/createContent/blocks/dcs/store'
import { useMeetingLiveVideoStore } from '../create/createContent/blocks/liveVideo/store'
import { useMeetingAiStore } from '../create/createContent/blocks/ai/store'
import { useMeetingPollStore } from '../create/createContent/blocks/poll/store'
import CreateContent from '../create/createContent'
import { Confirm } from '@/components/Alert'
import { fetchGetPersonalTemplate } from '@/api/template'
import { transSelectUserItem } from '../create/createContent/blocks/basicInfo/translator'
import { changeMeetingType, initMeetingTypeList } from '../create/createContent/blocks/meetingParams/store.combine'
import { userEdit, userInit } from '../create/createContent/blocks/user/store.combine'
import { useMeetingVmpStore } from '../create/createContent/blocks/vmp/store'
import { t } from 'i18next'
import { Message } from '@kedacom/react-next'
import { useMeetingTVWallStore } from '../create/createContent/blocks/tvwall/store'
import { initReactVmp } from '../create/createContent/blocks/vmp/store.combine'

/*
# 跳转到edit.html
1.模板列表页面 -- 点击新增按钮
jumpPage('edit')

2.模板列表页面 -- 点击编辑按钮
window.location.href = './edit.html?templateMoid=' + selectedRowIds[0]


*/
const templateMoid = getUrlParams('templateMoid') ?? ''

function App() {
	const { user, serverInfo } = useGlobalStore(
		(state) => ({ user: state.user, serverInfo: state.serverInfo }),
		shallow
	)
	const { update } = useCreateContentStore((state) => ({ update: state.update }), shallow)

	useInit()
	useEffect(() => {
		handleInit()
	}, [user, serverInfo])

	const handleInit = async () => {
		if (!user || !serverInfo) return

		let m = Message.loading(t('加载中，请稍候……'))
		update({ meetingMoid: templateMoid, isTemplate: true })
		//更新权限
		useCreateContentStore.getState().initPermission()
		let _preKey = await fetchPreKey(templateMoid, 2)
		update({ preKey: _preKey })

		if (templateMoid) {
			//更新模板
			let _model = await fetchGetPersonalTemplate(templateMoid)
			if (!_model.success) {
				Confirm(t('当前模板已被删除！'), () => {
					window.location.href = './meetingList.html'
				})
				return
			}
			//初始化基本信息
			useBasicInfoStore.getState().init({ isTemplate: true, model: _model })
			//初始化会议参数
			initMeetingTypeList() //更新会议类型列表
			useMeetingParamsStore.getState().init(_model)

			useMeetingPollStore.getState().init(_model.poll) //初始化轮询
			useMeetingMixStore.getState().init(_model.mix) //初始化混音
			useMeetingDcsStore.getState().init(_model.dcs) //初始化数据协作
			useMeetingLiveVideoStore.getState().init(_model.recorder) //初始化直播与录播
			useMeetingAiStore.getState().init(_model.ai) //初始化ai
			initReactVmp(_model.vmp) //初始化vmp
			useMeetingTVWallStore.getState().init(_model.tv_wall)
			//初始化user
			userEdit({
				all: _model.all_members ?? [],
				often: _model.often_mts ?? [],
			})
		} else {
			//创建模板
			//初始化基本信息
			useBasicInfoStore.setState({
				isInit: false,
				manager: transSelectUserItem({
					account: user.moid,
					account_type: 1,
					name: user.name,
				}),
			})

			//初始化会议参数
			let defaultMeetingType = initMeetingTypeList() as 0 | 1 | 2
			changeMeetingType(defaultMeetingType)
			useMeetingParamsStore.setState({ sourceUsed: 1 }) //新建的情况下传统模式默认资源预占
			useMeetingParamsStore.setState({
				rate:
					defaultMeetingType === 2
						? useCreateContentStore.getState().permission.enableHDMeeting
							? 4096
							: 2048
						: 2048,
			}) //rate未及时更新，先做赋值操作
			useMeetingPollStore.getState().init(null) //初始化轮询
			useMeetingMixStore.getState().init(null) //初始化混音
			useMeetingDcsStore.getState().init(null) //初始化数据协作
			useMeetingLiveVideoStore.getState().init(null) //初始化直播与录播
			useMeetingAiStore.getState().init(null) //初始化ai
			initReactVmp(null) //初始化vmp
			useMeetingTVWallStore.getState().init(null)
			//初始化user
			userInit({
				user: {
					account: user.moid,
					account_type: 1,
					name: user.name,
					protocol: 255,
					e164: user.e164,
					portrait: user.portrait32,
					bitrate: useMeetingParamsStore.getState().rate,
				},
			})
		}
		m.hide()
	}

	return (
		<>
			<WebRecommendBrowser />
			<MeetingHeader tabIndex={4} isShowCreateBtn={false} />
			<CreateContent />
		</>
	)
}

export default App
